package com.zy.modules.sys.service.impl;

import com.google.gson.Gson;
import com.zy.common.exception.RRException;
import com.zy.modules.sys.entity.SysConfigEntity;
import com.zy.modules.sys.repo.ConfigDao;
import com.zy.modules.sys.repo.ConfigRepository;
import com.zy.modules.sys.service.SysConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("sysConfigService")
public class SysConfigServiceImpl implements SysConfigService {
    @Resource
    private ConfigRepository configRepository;
    @Resource
    private ConfigDao configDao;

    @Override
    @Transactional
    public void save(SysConfigEntity config) {
        configRepository.save(config);
    }

    @Override
    public void update(SysConfigEntity config) {
        configRepository.save(config);
    }

    @Override
    public void updateValueByKey(String key, String value) {
        configDao.update(key, value);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        for (Long id : Arrays.asList(ids)) {
            configRepository.delete(id);
        }
    }

    @Override
    public List<SysConfigEntity> queryList(Map<String, Object> map) {
        int limit = (int) map.getOrDefault("limit", 10);
        int offset = (int) map.getOrDefault("offset", 0);
        String key = (String) map.getOrDefault("key", "");
        Pageable pageable = new PageRequest(offset, limit);
        Page<SysConfigEntity> entities = configRepository.findAll((root, cq, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(key)) {
                predicates.add(cb.like(root.get("key").as(String.class), "%" + key + "%"));
            }
            return cq.where(predicates.toArray(new Predicate[predicates.size()])).getGroupRestriction();
        }, pageable);
        return entities.getContent();
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        String key = (String) map.getOrDefault("key", "");
        long count = configRepository.count((root, cq, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(key)) {
                predicates.add(cb.like(root.get("key").as(String.class), "%" + key + "%"));
            }
            return cq.where(predicates.toArray(new Predicate[predicates.size()])).getGroupRestriction();
        });
        return (int) count;
    }

    @Override
    public SysConfigEntity queryObject(Long id) {
        return configRepository.findOne(id);
    }

    @Override
    public String getValue(String key, String defaultValue) {
        SysConfigEntity entity = configRepository.findByKey(key);
        String value = entity.getValue();
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return value;
    }

    @Override
    public <T> T getConfigObject(String key, Class<T> clazz) {
        String value = getValue(key, null);
        if (StringUtils.isNotBlank(value)) {
            return new Gson().fromJson(value, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RRException("获取参数失败");
        }
    }
}
